<?php
	session_start();
?>
<?php 
	if(!isset($_SESSION['userid']) || $_SESSION["loginas"]!=1)
	{
		header("location: ../");
	}
?>

<?php
	include("../functions.php");
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>Employer Section</title>
		<link href="../css/bootstrap.css" rel="stylesheet"/>
		<link href="../css/bootstrap-responsive.css" rel="stylesheet"/>
		<link href='../css/custom.css' rel='stylesheet'/>
		<script src="../js/jquery-1.9.1.js"></script>
		<script>
			window.onload=function(){
				//document.querySelector("#nav li:first-child").classList.add("active");
			};
			$(document).ready(function(){
				$("#btncancelprofile").click(function(){
					$("input[name='fname']").val("");
					$("input[name='position']").val("");
					$("input[name='email']").val("");
					$("input[name='pass']").val("");
					$("input[name='fname']").focus();
				});
				
				$("input[data]").click(function(){
					var jid = $(this).attr("data");
					$("#jid").val(jid);
				});
			});
		</script>
		<style type='ext/css'>
			
		</style>
	</head>
	<body>
		
		<div class="container">
			<div class='row-fluid row-header'>
			<div class='span3 text-center'>
			<a href='/cambodiajob/index.php'>
			<img src='/cambodiajob/img/logo_kh.png' alt='Khschools.com' />
			</a></div>
			<div class='span9'>
			<ul class='nav nav-pills pull-right' id='nav'>
				<li><a href='/cambodiajob/index.php' class='text-white'>Home</a></li>
				<li><a href='/cambodiajob/about.php' class='text-white'>About Us</a></li>
				<li><a href='/cambodiajob/#' class='text-white'>Contact Us</a></li>
				<li class='active'><a href='/cambodiajob/employer' class='text-white'>Profile</a></li>
				<li><a href="../logout.php" class="text-white" id="btnlogout">Logout</a></li>
				</ul></div></div>
		</div>
		
		
		<div class='container' style='margin-top: 18px;'>
			<div class='row-fluid'>
				<h3 class="text-success">Welcome to Member Section!</h3>
				<hr/>
			</div>
			<!--  Left menu for user profile -->
			<div class="row-fluid">
				<div class="span3">
					<form name="frm-menu" method="post">
						<ul style="list-style: none">
							<li><i class='icon-music'></i>
							<input type="submit" value="View Profile" name="profile" class="btn btn-link" /></li>
							<li><i class='icon-heart'></i>
							<input type="submit" value="Edit Profile" name="editprofile" class="btn btn-link" /></li>
							<li><i class='icon-envelope'></i>
							<input type="submit" value="View Jobs" name="job" class="btn btn-link" /></li>
							<li><i class='icon-list'></i>
							<input type='submit' value='Post Job' name='postjob' class='btn btn-link' />
							</li>
						</ul>
					</form>
					
				</div>
				<div class="span9">
				<!-- View user profile when user click view profile menu on the left side bar -->
					<?php 
						if(isset($_POST['profile'])){
							// get user information
							$result = runQuery("select * from employer where id=".$_SESSION['userid']);
							// display information
							echo "<h5 class='text-info'>User Profile</h5>";
							if(mysqli_num_rows($result)>0){
								echo "<table class='table'>";
								while($row=mysqli_fetch_array($result)){
									echo "<tr>";
									echo "<td>Full Name</td>";
									echo "<td>" . $row[1]."</td>";
									echo "</tr>";
									echo "<tr>";
									echo "<td>Postion</td>";
									echo "<td>".$row[2]."</td>";
									echo "</tr>";
									echo "<tr>";
									echo "<td>E-mail</td>";
									echo "<td>".$row[4]."</td>";
									echo "</tr>";
								}
								echo "</table>";
							}
						}
						if(isset($_POST['editprofile'])){
							// get user information
							$result = runQuery("select * from employer where id=".$_SESSION['userid']);
							if(mysqli_num_rows($result)>0){
								echo "<h5 class='text-info'>Edit Profile</h5>";
								echo "<form name='frm1' method='post'>";
								echo "<table class='table'>";
								$row = mysqli_fetch_array($result);
								echo "<tr>";
								echo "<td>Full Name</td>";
								echo "<td><input type='text' size='25' name='fname' value='{$row[1]}' /></td>";
								echo "</tr>";
								echo "<tr>";
								echo "<td>Position</td>";
								echo "<td><input type='text' size='25' name='position' value='{$row[2]}' /></td>";
								echo "</tr>";
								echo "<tr>";
								echo "<td>E-mail</td>";
								echo "<td><input type='text' size='25' name='email' value='{$row[4]}' /></td>";
								echo "</tr>";
								echo "<tr>";
								echo "<td>Password</td>";
								echo "<td><input type='password' size='25' name='pass' value='{$_SESSION['pass']}' /></td>";
								echo "</tr>";
								echo "<tr>";
								echo "<td>&nbsp;</td>";
								echo "<td><input type='submit' value='Save' name='btnsaveprofile' class='btn btn-primary' />
									<input type='button' value='Cancel' name='btncancelprofile' id='btncancelprofile' class='btn btn-danger'/></td>";
								echo "</tr>";
								echo "</table>";
								echo "</form>";
							}
						}
						///////////////////////////////////////////////////////////////////////////
						// save user profile
						
						if(isset($_POST['btnsaveprofile'])){
							$uid = $_SESSION['userid'];
							$fname = $_POST['fname'];
							$position = $_POST['position'];
							$email = $_POST['email'];
							$pass = $_POST['pass'];
							$sql ="update employer set fullname='$fname', position='$position', email='$email', pass=md5($pass) where id=$uid";
							if(runNonQuery($sql)==1){
								echo "<p class='text-info'>Data has been saved!</p>";
								
							}
							else
								echo "<p class='text-error'>Cannot save data!</p>";
						}
						
						// view job when user clicks the view job menu
						if(isset($_POST['job'])){
							echo "<h5 class='text-info'>View Jobs</h5>";
							$empId = $_SESSION['userid'];
							$jobs = getJobByEmployer($empId);
							if(mysqli_num_rows($jobs)>0){
								echo "<form method='post'>";
								echo "<input type='hidden' name='jid' id='jid' />";
								echo "<table class='table'>";
								echo "<tr>";
								echo "<th>Job ID</th><th>Job Title</th><th>Desciption</th><th>Actions</th>";
								echo "</tr>";
								while($job=mysqli_fetch_array($jobs)){
									echo "<tr>";
									echo "<td>".$job[0]."</td>";
									echo "<td>".$job[1]."</td>";
									echo "<td>".$job[2]."</td>";
									echo "<td>"."<input data='$job[0]' type='submit' value='Edit' name='btnEditJob' class='btn btn-link text-info' />"."</td>";
									echo "</tr>";
								}
								echo "</table>";
								echo "</form>";
							}
						}
						
						// when user click edit job button, create an edit form for them
						if(isset($_POST['btnEditJob'])){
							
							echo $_POST['jid'];
						}
					?>
				</div>
			</div>
	</div>
	</body>
</html>
